<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/12/3
 * Time: 10:27
 */

namespace Api\Controller;
use Api\Model\ErrorCodeModel;
use Api\Vendor\BoyeUtils\BoyeUtils;
use OAuth2Manage\Api\AccessTokensApi;


/**
 * 对外调用的唯一接口
 * Class GatewayController
 * @package Api\Controller
 */
class IndexController extends ApiController {

    private $time;
    private $data;//加密过的数据
    private $type;
    private $sign;
    private $alg;
    private $api_ver;//当前接口的版本，数字从100开始计数
    private $app_version;//当前软件的版本
    private $app_type;//当前软件的类型 ，ios，android

    private $decrypt_data; //解密过的数据

    public function index(){


//        $api_type = str_replace("_","/",ltrim($this->type,"BY_"));

        $api_type = preg_replace("/_/","/",substr(ltrim($this->type),3),1);

        $api_type = preg_split("/\//",$api_type);

        if(count($api_type) < 2){
            $this->apiReturnErr("type参数不正确!",ErrorCodeModel::Invalid_Parameter);
        }

        $action_name = $api_type[1];
        $controller_name = $api_type[0];

        $domainClass = $controller_name.'/'.$action_name;
        addLog($domainClass,$_GET,$_POST,"[接口调用开始]");

        $_POST = $this->decrypt_data;
        $_POST['domain_class'] = $domainClass;
        $result = apiCall($domainClass,array(),'Domain');
        addLog($domainClass,$result,$_POST,"[接口调用结果]");

        if(!$result['status']){
            $this->apiReturnErr($result['info'],ErrorCodeModel::Business_Error);
        }

        //1. 这一步不会走到
        $this->apiReturnErr("无法处理!",ErrorCodeModel::Business_Error);

    }







    protected function _init(){

        $ignore_access_token = true;//不检测access_token

        $this->initParameter();

        $access_token = $this->_get("access_token","");
        if(empty($access_token)){
            $access_token = $this->_post("access_token","");
            unset($_POST['access_token']);
        }

        if(empty($access_token)){
            $this->apiReturnErr("缺失access_token!",ErrorCodeModel::Lack_Parameter);
        }

        $_GET['access_token'] = $access_token;

        if(!$ignore_access_token){


            $resCtrl = new ResourceController();

            $result = $resCtrl->authorize();

            if($result['status'] !== 0){
                $this->apiReturnErr($result['info'],$result['status']);
            }

            $result = apiCall(AccessTokensApi::GET_INFO,array(array('access_token'=>$access_token)));

            if($result['status']){
                $this->client_id = $result['info']['client_id'];
            }

        }else{

//            if(!isset($_POST['client_id'])) {
//                $this->apiReturnErr("client_id参数缺失!",ErrorCodeModel::Lack_Parameter);
//            }

            $this->client_id = $access_token;
            unset($_POST['client_id']);

        }

        $this->check();
        $this->getConfig();
    }




    //==私有方法


    /**
     * 初始化公共参数
     */
    private function initParameter(){

        //1. 获取time参数
        if(isset($_POST['time'])) {
            //如果传了，则检测时间范围，如果不传就不检测，针对android
            $this->time = intval($_POST['time']);
            unset($_POST['time']);
            if (!is_int($this->time)) {
                $this->apiReturnErr("时间戳格式非法!", ErrorCodeModel::Invalid_Parameter);
            }
        }else{
            $this->apiReturnErr("time参数缺失!",ErrorCodeModel::Lack_Parameter);
        }

        //2. 获取sign参数
        if(!isset($_POST['sign'])) {
            $this->apiReturnErr("sign参数缺失!",ErrorCodeModel::Lack_Parameter);
        }
        $this->sign = $_POST['sign'];
        unset($_POST['sign']);

        //3. 获取data参数
        if(!isset($_POST['data'])) {
            $this->apiReturnErr("data参数缺失!",ErrorCodeModel::Lack_Parameter);
        }
        $this->data = $_POST['data'];
        unset($_POST['data']);

        //4.获取type参数
        if(!isset($_POST['type'])) {
            $this->apiReturnErr("type参数缺失!",ErrorCodeModel::Lack_Parameter);
        }
        $this->type = $_POST['type'];
        unset($_POST['type']);

        //5.获取type参数
        if(!isset($_POST['alg'])) {
            $this->apiReturnErr("alg参数缺失!",ErrorCodeModel::Lack_Parameter);
        }
        $this->alg = strtolower($_POST['alg']);

        if(!in_array($this->alg,array('md5'))){
            $this->apiReturnErr("alg=".$this->alg.",参数非法!",ErrorCodeModel::Invalid_Parameter);
        }

        unset($_POST['alg']);


        //5.获取notify_id参数
        if(!isset($_POST['notify_id'])) {
            $this->apiReturnErr("notify_id参数缺失!",ErrorCodeModel::Lack_Parameter);
        }
        $this->notify_id = $_POST['notify_id'];
        unset($_POST['notify_id']);

        //6.获取notify_id参数
        if(!isset($_POST['api_ver'])) {
            $this->apiReturnErr("api_ver参数缺失!",ErrorCodeModel::Lack_Parameter);
        }
        $this->api_ver = $_POST['api_ver'];
        unset($_POST['api_ver']);

        //7.获取 client_id 参数

        $this->app_version = $_POST['app_version'];
        $this->app_type = $_POST['app_type'];

    }

    /**
     * 解密验证
     */
    private function check(){

        //1. 请求时间戳校验
        $now = time();
        //时间误差 +-5 分钟
        if($now - 5*60 > $this->time || $this->time > $now + 5*60){
            $this->apiReturnErr("该请求时间已失效!",ErrorCodeModel::Invalid_Parameter);
        }

        //2. 签名校验
        $param = array(
            'client_id'=>$this->client_id,
            'notify_id'=>$this->notify_id,
            'time'=>$this->time,
            'data'=>$this->data,
            'type'=>$this->type,
            'alg'=>$this->alg,
        );
        if(!BoyeUtils::verify_sign($this->sign,$param)){
            $this->apiReturnErr("签名验证错误!");
        }

        //3. 数据解密
        $this->decrypt_data = $param;
        $this->decrypt_data['api_ver'] = $this->api_ver;
        $this->decrypt_data['client_id'] = $this->client_id;
        $this->decrypt_data['app_version'] = $this->app_version;
        $this->decrypt_data['app_type'] = $this->app_type;

        $data = BoyeUtils::decrypt($this->data);
        foreach($data as $key=>$vo){
            $this->decrypt_data['_data_'.$key] = $vo;
        }
    }



}